import {Component, OnInit} from "@angular/core";
import {
  NzNotificationService,
  NzMessageService
} from "ng-zorro-antd";
import {Router} from "@angular/router";
import {HttpServcie} from "../../../service/http.service";
import {Config} from "../../../service/config";
import {ElevatorNoownerInfoBean} from "./bean/elevatorNoownerInfo.bean";
import {NzModalService, NzModalSubject} from "ng-zorro-antd";
import {elevatorTypes} from "../../../service/constant/elevatorTypeConstant";
import {ElevatorInfoBean} from "../elevator-info/bean/elevatorInfo.bean";


@Component({
  selector: "elevator-noowner",
  templateUrl: "./elevator-noowner.html",
  styleUrls: ["./elevator-noowner.scss"]
})
/**
 * 电梯信息列表
 */
export class NooWnerInfoPage implements OnInit {
  public flag: boolean = true;
  data: any;
  list: any = []; //返回的数据

  modal: any = 0;
  elevatorlBean: ElevatorNoownerInfoBean = new ElevatorNoownerInfoBean();
  currentModal: NzModalSubject;
  modelTitle:"修改";
  updateData: any = [];
  updateList: any = [];
  total: string;
  elevatorTypes = elevatorTypes;
  config: any = Config;

  //多选框
  allChecked: any = false;
  indeterminate: any = false;
  nzSpinning: boolean = false; //遮罩
  partyId = JSON.parse(window.localStorage["user"]).partyId;
  partyType = JSON.parse(window.localStorage["user"]).partyType;


  ngOnInit() {

  }

  constructor(
    private httpServcie: HttpServcie,
    private router: Router,
    private modalService: NzModalService,
    private _notification: NzNotificationService,
  ) {
    this.elevatorlBean.pageSize = 10;
    this.elevatorlBean.currentPage = 1;
    this.queryUp();
  }

  //全选
  checkAll(value) {
    if (value) {
      this.list.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.list.forEach(data => (data.checked = false));
    }
  }

    //分页
    nzPageSizeChange(event){
      this.elevatorlBean.pageSize = event;
      this.elevatorlBean.currentPage = 1;
      this.queryUp();
    }

    //输入框的方法
    nzPageIndexChange(event) {
      this.elevatorlBean.currentPage = event;
      this.queryUp();
    }

    //当前页
    nzPageIndexClickChange(event) {
      this.elevatorlBean.currentPage = event;
      this.queryUp();
    }

  /**
   * 查询时调用
   */
  queryUp() {
    this.nzSpinning = true;
    let param = {
      pageSize: this.elevatorlBean.pageSize,
      currentPage: this.elevatorlBean.currentPage,
      elevatorNum: this.elevatorlBean.elevatorNum,//外部关联码或设备代码
      useUnit: this.elevatorlBean.useUnit,//使用单位
      maintenanceUnit: this.elevatorlBean.maintenanceUnit,//维保单位

    }
    this.httpServcie.queryNoownerElevator(param).subscribe(
      data => {
        if (data.status == 200) {
          this.nzSpinning = false;
          this.list = data.data.list;
          this.total = data.data.total;

        } else if (data.status === 401) {
          this.router.navigateByUrl("/login");
          return;
        } else {
          // 异常
          this._notification.create("error", "错误提示", data.message);
        }
      },
      error => {
        console.log(JSON.stringify(error));
      }
    );
  }

  update(titleTpl, contentTpl, footerTpl) {
    this.modelTitle = "修改";
    this.modal = 1;
    const updateList: any = [];

    this.list.forEach(element => {
      if (element.checked) {
        updateList.push(element.id);
        this.updateData = updateList;
      }
    });

    if (updateList.length == 0) {
      // 异常
      this._notification.create("warning", "警告提示", "请选中一条数据！");
      return;
    }

    this.currentModal = this.modalService.open({
      title: titleTpl,
      content: contentTpl,
      footer: footerTpl,
      maskClosable: false,
      width: 900,
      style: {
        top: "20px"
      },
      onOk() {
        console.log("Click ok");
      }
    });
  }

  // 添加后界面刷新
  addOutPut(event) {
    setTimeout(() => {
      this.currentModal.destroy();
      this.currentModal = null;
    }, 200);
    this.queryUp();
  }
}
